package com.hospital.wx.controller;

import com.hospital.wx.common.CommonResult;
import com.hospital.wx.controller.form.SelectDoctorInfoByIdForm;
import com.hospital.wx.pojo.Doctors;
import com.hospital.wx.service.DoctorsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/doctors")
@Tag(name = "DoctorsController", description = "医生信息接口管理")
@Slf4j
public class DoctorsController {
    @Autowired
    private DoctorsService doctorsService;

    @PostMapping("/selectTelByNameOrDeptName")
    @Operation(summary = "根据医生姓名或者科室查询医生信息")
    @CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
    public CommonResult selectTelByNameOrDeptName(@RequestParam(value = "keyword", required = false) String keyword) {
        try {
            HashMap<String, Doctors> doctors = doctorsService.selectTelByNameOrDeptName(keyword);
            return CommonResult.ok().put("doctors", doctors);
        } catch (Exception e) {
            e.printStackTrace(); // 打印异常堆栈信息
            return CommonResult.failed("查询失败：" + e.getMessage());
        }
    }

    @GetMapping("/selectAll")
    @Operation(summary = "查询所有医生")
    @CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
    public CommonResult selectAll() {
        List<HashMap> maps = doctorsService.selectAll();
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, maps);
    }

    @PostMapping("selectDoctorInfoById")
    @Operation(summary = "查询医生详细信息")
    public CommonResult selectDoctorInfoById(@RequestBody @Valid SelectDoctorInfoByIdForm form) {
        try {
            HashMap map = doctorsService.selectDoctorInfoById(form.getId());
            return CommonResult.ok().put(CommonResult.RETURN_RESULT, map);
        } catch (Exception e) {
            log.error("查询医生详细信息过程中出现异常", e);
            return CommonResult.failed("查询医生详细信息过程中出现异常");
        }
    }
}
